<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8" />
    <title>Refresher - Basic</title>
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <link href="../../../../../css/ionic.bundle.css" rel="stylesheet" />
    <link href="../../../../../scripts/testing/styles.css" rel="stylesheet" />
    <script src="../../../../../scripts/testing/scripts.js"></script>
    <script nomodule src="../../../../../dist/ionic/ionic.js"></script>
    <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
  </head>

  <body>
    <ion-app>
      <ion-header>
        <ion-toolbar>
          <ion-title>Pull To Refresh</ion-title>
        </ion-toolbar>
      </ion-header>

      <ion-content>
        <ion-refresher id="refresher" slot="fixed">
          <ion-refresher-content
            pulling-icon="arrow-down-outline"
            pulling-text="Pull to refresh..."
            refreshing-text="Refreshing..."
            refreshing-spinner="circles"
          >
          </ion-refresher-content>
        </ion-refresher>

        <ion-list id="list"></ion-list>
      </ion-content>
    </ion-app>
    <ion-menu-controller></ion-menu-controller>

    <script>
      let items = [];
      for (var i = 0; i < 30; i++) {
        items.push(i + 1);
      }

      const list = document.getElementById('list');
      const refresher = document.getElementById('refresher');

      refresher.addEventListener('ionRefresh', async function () {
        const data = await getAsyncData();
        items = items.concat(data);
        refresher.complete();
        render();
        // Custom event consumed by e2e tests
        window.dispatchEvent(new CustomEvent('ionRefreshComplete'));
      });

      function render() {
        let html = '';
        for (let item of items) {
          html += `<ion-item button>${item}</ion-item>`;
        }
        list.innerHTML = html;
      }

      function getAsyncData() {
        // async return mock data
        return new Promise((resolve) => {
          setTimeout(() => {
            let data = [];
            for (var i = 0; i < 30; i++) {
              data.push(i);
            }
            resolve(data);
          }, 500);
        });
      }

      render();
    </script>
  </body>
</html>
